
local TankShooting = class("TankShooting", require("unity.LuaMonoBehaviour"))

---
---mPlayerNumber number
---mShell Rigidbody
---mFireTransform Transform
---mAimSlider Slider
---mShootingAudio AudioSource
---mChargingClip AudioClip
---mFireClip AudioClip
---mMinLaunchForce number
---mMaxLaunchForce number
---mMaxChargeTime number
---
function TankShooting:Awake()
    self.mFireButton = nil
    self.mCurrentLaunchForce = 0
    self.mChargeSpeed = 0
    self.mFired = false
end

function TankShooting:OnEnable()
    self.mCurrentLaunchForce = self.mMinLaunchForce
    self.mAimSlider.value = self.mMinLaunchForce
end

function TankShooting:Start()
    self.mFireButton = "Fire" .. tostring(self.mPlayerNumber) 
    self.mChargeSpeed = (self.mMaxLaunchForce - self.mMinLaunchForce) / self.mMaxChargeTime
end

function TankShooting:Update()
    self.mAimSlider.value = self.mMinLaunchForce
    if self.mCurrentLaunchForce >= self.mMaxLaunchForce and not self.mFired then
        self.mCurrentLaunchForce = self.mMaxLaunchForce
        self:Fire ()
    elseif UnityEngine.Input.GetButtonDown (self.mFireButton) then
        self.mFired = false
        self.mCurrentLaunchForce = self.mMinLaunchForce
        self.mShootingAudio.clip = self.mChargingClip
        self.mShootingAudio:Play ()
    elseif UnityEngine.Input.GetButton (self.mFireButton) and not self.mFired then
        self.mCurrentLaunchForce = self.mCurrentLaunchForce + self.mChargeSpeed * Time.deltaTime
        self.mAimSlider.value = self.mCurrentLaunchForce
    elseif UnityEngine.Input.GetButtonUp (self.mFireButton) and not self.mFired then
        self:Fire ()
    end
end

function TankShooting:Fire()
    self.mFired = true
    local shellInstance = UnityEngine.Object.Instantiate (self.mShell, self.mFireTransform.position, self.mFireTransform.rotation)
    shellInstance.velocity =  self.mFireTransform.forward:Mul(self.mCurrentLaunchForce)
    self.mShootingAudio.clip = self.mFireClip
    self.mShootingAudio:Play ()
    self.mCurrentLaunchForce = self.mMinLaunchForce
end

return TankShooting
